package com.pur.zsms.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pur.zsms.pmsnmanage.user.po.UserPo;

public class SessionFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void destroy() {
		System.out.println("SessionFilter终止");
	}

	/**
	 * 拦截所有的HTTP请求, 并进行合法性检查<br>
	 * 
	 * @param servletRequest
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpSession session = request.getSession();
		System.out.println(request.getServletPath() + "------------");
		// 如果左等于/login /index.jsp和/nosession.jsp, 直接通过
		if (request.getServletPath().startsWith("/user/login.do") || request.getServletPath().equals("/jsp/login.jsp")
				|| request.getServletPath().startsWith("/jsp/nosession.jsp") || request.getServletPath().startsWith("/jsp/logout.jsp")
				|| request.getServletPath().startsWith("/jsp/indexMobile.jsp")
				|| request.getServletPath().startsWith("/jsp/indexMobileForCashier.jsp")
				|| request.getServletPath().startsWith("/MP_verify_Ud88SOQJsay8uwqy.txt") || request.getServletPath().startsWith("/weixintest/")
				|| request.getServletPath().startsWith("/jsp/weixinlogin.jsp") || request.getServletPath().startsWith("/jsp/weixinJump.jsp")
				|| request.getServletPath().startsWith("/user/weixin.do")) {
			chain.doFilter(request, response);
			return;
		}

		// 微信部分直接通过
		if (request.getServletPath().startsWith("/weixintest/") || request.getServletPath().startsWith("/weixinOrder/") || request.getServletPath().startsWith("/jsp/weixinlogin.jsp")
				|| request.getServletPath().startsWith("/jsp/weixinJump.jsp") || request.getServletPath().startsWith("/jsp/weixinMenuForOut.jsp")
				|| request.getServletPath().startsWith("/jsp/weixin/message/NoHaveUser.jsp")
				|| request.getServletPath().startsWith("/jsp/weixin/message/successWeixin.jsp")
				|| request.getServletPath().startsWith("/jsp/weixinJumploginOut.jsp")
				|| request.getServletPath().startsWith("/jsp/weixinJumpOrderForSupplier1.jsp")
				|| request.getServletPath().startsWith("/jsp/weixinJumpOrderForSupplier2.jsp")
				|| request.getServletPath().startsWith("/jsp/weixinJumpOrderForSupplier3.jsp")
				|| request.getServletPath().startsWith("/jsp-mobile/customOrderForSup_mobile.jsp")
				|| request.getServletPath().startsWith("/weixintest/weixinOrderForSupToCust.do")
				|| request.getServletPath().startsWith("/user/weixin.do")) {
			chain.doFilter(request, response);
			return;
		}
		// 如果左等于/images访问, 直接通过
		if (request.getServletPath().startsWith("/images") || request.getServletPath().startsWith("/static")) {
			chain.doFilter(request, response);
			return;
		}

		// 如果用户尚未登录, 转向登录页面
		UserPo user = (UserPo) session.getAttribute("user");
		if (user == null) {
			httpResponse.sendRedirect(request.getContextPath() + "/jsp/nosession.jsp ");
		}
		chain.doFilter(request, response);
	}

}
